$.ajaxSetup({
	xhrFields: {
		withCredentials: true
	}
});
var vm = {
	belong: 'report',
	//任务id
	testItemId: '',
	//左侧参数列表
	selections:{
		nowTab: 'sampleFields',
		nowData:[],
		isAjax: false,
		childdren:[],
		showData:{
			baseFields:{
				start: 0,
				isEnd: false,
				data: [],
				getFields: null,
				childdren:[
					{
						name: '产品',
						isOpen: true,
						data: [
							{name: '产品名称',codeText: '{{sample.sampname}}',belong: 'report,record'},
							{name: '型号规格',codeText: '{{sample.carmodel}}',belong: 'report,record'},
							{name: '受检单位',codeText: '{{sample.customername}}',belong: 'report,record'},
							{name: '商标',codeText: '{{sample.sampversion}}',belong: 'report,record'},
							{name: '受检单位',codeText: '{{sample.customername}}',belong: 'report,record'},
							{name: '生产单位',codeText: '{{sample.sampproducer}}',belong: 'report,record'},
							{name: '送样者',codeText: '{{sample.sender}}',belong: 'report,record'},
							{name: '送样日期',codeText: '{{-sample.senddate}}',belong: ' report,record'},
							{name: '样品数量',codeText: '{{sample.sampnumber}}',belong: 'report,record'},
							{name: '样品编号',codeText: '{{sample.sampcode}}',belong: 'report,record'},
							{name: '出厂编号',codeText: '{{sample.sampdeliverycode}}',belong: 'report,record'},
							{name: '样品状况',codeText: '{{sample.sampcondition}}',belong: 'report,record'},
							{name: '生产日期',codeText: '{{-sample.producedate}}',belong: 'report,record'}
						]
					},
					{
						name: '客户',
						isOpen: true,
						data: [
							{name: '客户名称',codeText: '{{custOrg.name}}',belong: 'report,record'},
							{name: '客户地址',codeText: '{{custOrg.address}}',belong: 'report,record'},
							{name: '电话',codeText: '{{custOrg.phone}}',belong: 'report,record'},
							{name: '邮政编码',codeText: '{{custOrg.postcode}}',belong: 'report,record'},
							{name: '传真',codeText: '{{custOrg.fax}}',belong: 'report,record'}
						]
					},
					{
						name: '报告',
						isOpen: true,
						data: [
							{name: '编号',codeText: '{{reportNo}}',belong: 'report'},
							{name: '编号',codeText: '{{report.reportNo}}',belong: 'record'}
						]
					},
					{
						name: '任务',
						isOpen: true,
						data: [
							{name: '承接部门',codeText: '{{dutyOrgName}}',belong: 'report,record'}
						]
					},
					{
						name: '公共',
						isOpen: true,
						data: [
							{name: '检测地点',codeText: '{{testAddress}}',belong: 'report,record'},
							{name: '设备使用记录',codeText: '{{assetsUse}}',belong: 'report,record'},
							{name: '实验人员',codeText: '{{tester}}',belong: 'report,record'},
							{name: '检测开始时间',codeText: '{{-testStartTime}}',belong: 'report,record'}
						]
					},
					{
						name: '签名',
						isOpen: true,
						data: [
							{name: '主检',codeText: '{{majorPosition}}',belong: 'report,record'},
							{name: '审核人',codeText: '{{approverPosition}}',belong: 'report,record'},
							{name: '批准人',codeText: '{{authorizerPosition}}',belong: 'report,record'}
						]
					},
					{
						name: '公章',
						isOpen: true,
						data: [
							{name: '首页公章',codeText: '{{1041}}',belong: '#'},
							{name: '签发日期公章',codeText: '{{1042}}',belong: '#'}
						]
					}
				],
			},
			ysFields:{
				start: 0,
				isEnd: false,
				data: [],
				getFields: null,
				childdren:[],
			},
			sampleFields:{
				start: 0,
				isEnd: false,
				data: [],
				getFields: null,
				childdren:[],
			},
			otherFields:{
				start: 0,
				isEnd: false,
				data: [],
				getFields: null,
				childdren:[],
			},
			xzFields:{
				start: 0,
				isEnd: false,
				data: [],
				getFields: null,
				childdren:[],
			},
		}
	},
	//加载需计算的样式
	loadCss: function(){
		$(".page-content").css("overflow","hidden");
		var portlet_h = $(".portlet").height();
		var title_h = $(".portlet-title").outerHeight(true);
		$(".portlet-body").height(portlet_h - title_h);

		var topp_h = $("#tab-content").parent().height();
		var top_h = $("#top-content").outerHeight(true);
		if(!top_h){
			top_h = 0;
		}
		$("#tab-content").height(topp_h - top_h);
	},
	//加载列表默认事件
	loadFun:function(){
		var $this = vm;
		$this.selections.showData.baseFields.getFields = function () {
			var htmlStr = '';
			var result = $this.getBaseFields();
			for(var i = 0; i < result.length; i++){
				var item = result[i],
					boo = false,
					data = [],
					childHtmlStr = '';
				for(var j = 0; j < item.data.length; j++){
					var cItem = item.data[j];
					if(cItem.belong.indexOf($this.belong) != -1){
						boo = true;
						data.push(cItem);
						childHtmlStr += $this.loadHtml('baseFields', false, cItem);
					}
				}
				if(boo){
					item.data = data;
					$this.selections.showData.baseFields.childdren.push(item);
					$this.selections.childdren.push(item);
					htmlStr += $this.loadHtml('baseFields', true, item);
					htmlStr += childHtmlStr;
					htmlStr += '</div>';
				}
			}
			$("#tab-body-content").append(htmlStr);
			$this.selections.showData.baseFields.isEnd = true;
		};
		$this.selections.showData.ysFields.getFields = function () {
			var url = "/sdm/recordDefinition/recordDataGrid?testItemId=" + $this.testItemId;
			url += "&length=20";
			url += "&start=" + $this.selections.showData.ysFields.start;
			$this.selections.isAjax = true;
			App.get(url,function (result){
				if(result){
					var dataType = 2;
					var htmlStr = '';
					if(result.childRecord){
						for(var i = 0; i < result.childRecord.length;i++){
							var cItem = result.childRecord[i],
								childHtmlStr = '';
							cItem.name = cItem.formName;
							cItem.data = [];
							cItem.isOpen = true;
							//是否动态行
							var isFixRow =false;
							// 判断是否动态行
							var properties =cItem.extendsPropertes;
							if(properties){
							   if(properties.fixedRow==1){
							    isFixRow =true;
							   }
							}
							for (var k = 0; k < cItem.recordColumns.length;k++) {
								var kItem = cItem.recordColumns[k];
								kItem.name = kItem.fieldName;
								if(isFixRow){ //动态行
									kItem.codeText = "{{$record." + cItem.code + "." + kItem.fieldCode + "}}";
								} else { //固定行
									kItem.codeText = "{{%record." + cItem.code + "." + kItem.fieldCode + "}}";
								}
								cItem.data.push(kItem);
								childHtmlStr += $this.loadHtml('ysFields', false, kItem);
							}
							$this.selections.childdren.push(cItem);
							$this.selections.showData.ysFields.childdren.push(cItem);
							htmlStr += $this.loadHtml('ysFields', true, cItem);
							htmlStr += childHtmlStr;
							htmlStr += '</div>';
						}

					}
					if(result.recordColumns){
						for(var i = 0; i < result.recordColumns.length; i++){
							var item = result.recordColumns[i];
							item.name = item.fieldName;
							if(parseInt(item.dataType) == dataType){
								item.codeText = "{{@record." + item.fieldCode + "}}";
							} else {
								item.codeText = "{{~record." + item.fieldCode + "}}";
							}
							$this.selections.showData.ysFields.data.push(item);
							$this.selections.nowData.push(result.recordColumns[i]);
							htmlStr += $this.loadHtml('ysFields', false, item);
						}
					}

					$this.selections.showData.ysFields.isEnd = true;
					// if($this.selections.showData.ysFields.start + 20 > result.total){
					// 	$this.selections.showData.ysFields.isEnd = true;
					// } else {
					// 	$this.selections.showData.ysFields.start += 20;
					// }
					$("#tab-body-content").append(htmlStr);
				}
				$this.selections.isAjax = false;
			} ,function (msg){
				// console.log(msg);
				$this.selections.isAjax = false;
			});
		};
		$this.selections.showData.sampleFields.getFields = function () {
			var url = "/md/testItemParam/paramGrid?testItemId=" + $this.testItemId;
			url += "&length=20";
			url += "&start=" + $this.selections.showData.sampleFields.start;
			$this.selections.isAjax = true;
			App.get(url,function (result){
				if(result){
					var htmlStr = '';
					for(var i = 0; i < result.rows.length; i++){
						result.rows[i].name = result.rows[i].paramName;
						result.rows[i].codeText = "{{~sampleParam." +result.rows[i].paramCode + "}}";
						$this.selections.showData.sampleFields.data.push(result.rows[i]);
						$this.selections.nowData.push(result.rows[i]);
						htmlStr += $this.loadHtml('sampleFields', false, result.rows[i]);
					}
					if($this.selections.showData.sampleFields.start + 20 > result.total){
						$this.selections.showData.sampleFields.isEnd = true;
					} else {
						$this.selections.showData.sampleFields.start += 20;
					}
					$("#tab-body-content").append(htmlStr);
				}
				$this.selections.isAjax = false;
			} ,function (msg){
				// console.log(msg);
				$this.selections.isAjax = false;
			});
		};
		$this.selections.showData.otherFields.getFields = function () {
			var url = "/sys/dictionary/dataGrid?typeId=2500&length=20";
			url += "&start=" + $this.selections.showData.otherFields.start;
			$this.selections.isAjax = true;
			App.get(url,function (result){
				if(result){
					var htmlStr = '';
					for(var i = 0; i < result.rows.length; i++){
						result.rows[i].name = result.rows[i].name;
						result.rows[i].codeText = "{{~other." +result.rows[i].name + "}}";
						$this.selections.showData.otherFields.data.push(result.rows[i]);
						$this.selections.nowData.push(result.rows[i]);
						htmlStr += $this.loadHtml('otherFields', false, result.rows[i]);
					}
					if($this.selections.showData.otherFields.start + 20 > result.total){
						$this.selections.showData.otherFields.isEnd = true;
					} else {
						$this.selections.showData.otherFields.start += 20;
					}
					$("#tab-body-content").append(htmlStr);
				}
				$this.selections.isAjax = false;
			} ,function (msg){
				// console.log(msg);
				$this.selections.isAjax = false;
			});
		};
		$this.selections.showData.xzFields.getFields = function () {
			$this.selections.showData.xzFields.isEnd = true;
			var url = '/sdm/limits/listAll?testItemId=' + $this.testItemId;
			$this.selections.isAjax = true;
			App.get(url,function (result){
				if(result){
					var xzItem = {name: '限值',data:[],isOpen: true},
						fhpdItem = {name: '符合判定',data:[],isOpen: true},
						xzStr = $this.loadHtml('xzFields', true, xzItem),
						fhpdStr = $this.loadHtml('xzFields', true, fhpdItem);
					for (var index = 0, item; item = result[index++];) {
						item.name = item.metaName;
						item.codeText = "{{~limits." + item.metaName + "}}";
						if (item.metaType == 1) {//限值
							xzItem.data.push(item);
							xzStr += $this.loadHtml('xzFields', false, item);
						} else {//符合判定
							fhpdItem.data.push(item);
							fhpdStr += $this.loadHtml('xzFields', false, item);
						}
					}
					$this.selections.childdren.push(xzItem);
					$this.selections.showData.xzFields.childdren.push(xzItem);
					$this.selections.childdren.push(fhpdItem);
					$this.selections.showData.xzFields.childdren.push(fhpdItem);
					xzStr += '</div>';
					fhpdStr += '</div>';
					$("#tab-body-content").append(xzStr + fhpdStr);
				}
				$this.selections.isAjax = false;
			} ,function (msg){
				// console.log(msg);
				$this.selections.isAjax = false;
			});
		};
		// $("#selections").on('click','.item',function(e){$this.mousedown(e);});
		$("#selections .tab-body").scroll($this.handleScroll);
		$(window).resize(function(){
			var timeout = setTimeout(function(){
				$this.loadCss();
				clearTimeout(timeout);
			},300);
		});
		
	},
	//参数列表滚动条滚动事件
	handleScroll: function(e){
		//变量scrollTop是滚动条滚动时，距离顶部的距离
		var scrollTop = e.target.scrollTop;
		//变量windowHeight是可视区的高度
		var windowHeight = e.target.clientHeight;
		//变量scrollHeight是滚动条的总高度
		var scrollHeight = e.target.scrollHeight;
		//滚动条到底部的条件
		if(scrollTop+windowHeight==scrollHeight && !vm.selections.isAjax){
			vm.getFields(vm.selections.nowTab);
			//写后台加载数据的函数
			// console.log("距顶部"+scrollTop+"可视区高度"+windowHeight+"滚动条总高度"+scrollHeight);
		}
	},
	//获取样品参数
	getFields: function(newTab) {
		if(vm.selections.isAjax){
			return;
		}
		if(vm.selections.nowTab == newTab && !vm.selections.showData[newTab].isEnd) {
			vm.selections.showData[newTab].getFields();
		} else {
			vm.selections.nowTab = newTab;
			vm.selections.nowData = vm.concat([],vm.selections.showData[newTab].data);
			vm.selections.nowStart = vm.selections.showData[newTab].start;
			vm.selections.childdren = vm.concat([],vm.selections.showData[newTab].childdren);
			if(vm.selections.nowData.length == 0 && !vm.selections.showData[newTab].isEnd) {
				vm.selections.showData[newTab].getFields();
			}
		}
	},
	//数组拼接
	concat: function(arr1, arr2){
		for(var i = 0; i < arr2.length; i++){
			arr1.push(arr2[i]);
		}
		return arr1;
	},
	//生成节点页面代码
	loadHtml: function (tab,isParent,item) {
		var htmlStr = '';
		if(isParent){
			htmlStr += '<div class="pItem ' + tab + '">';
			htmlStr += '<div class="pItem-title" isOpen="true" onclick="vm.openNode(this)">' + item.name + '<span class="glyphicon glyphicon-chevron-up" style="display: none;"></span><span class="glyphicon glyphicon-chevron-down"></span></div>';
		} else {
			htmlStr += '<div class="item ' + tab + '" onclick="vm.onItemClick(\'' + item.codeText + '\')">';
			htmlStr += item.name;
			htmlStr += '</div>';
		}
		return htmlStr;
	},
	//初始化
	onload: function (testItemId,belong) {
		vm.testItemId = testItemId;
		vm.belong = belong;
		vm.loadCss();
		vm.loadFun();
		vm.getFields(vm.selections.nowTab);
	},
	//打开或关闭子节点
	openNode: function (e) {
		var isOpen = $(e).attr("isOpen");
		if(isOpen != "true"){
			$(e).attr("isOpen", "true");
			$(e).find(".glyphicon-chevron-down").css("display","inline-block");
			$(e).find(".glyphicon-chevron-up").css("display","none");
			$(e).parent().find(".item").css("display","block");
		} else {
			$(e).attr("isOpen", "false");
			$(e).find(".glyphicon-chevron-down").css("display","none");
			$(e).find(".glyphicon-chevron-up").css("display","inline-block");
			$(e).parent().find(".item").css("display","none");
		}
	},
	//参数点击事件
	onItemClick: function (text) {
		var obj = document.getElementById("pageOfficeCtrl").Document;
		obj.Application.Selection.Text = text;
	},
	//tab标题的点击切换事件
	onTabClick: function (newTab,e) {
		if(vm.selections.nowTab != newTab) {
			$(e).parent().find(".cas").removeClass("cas");
			$(e).addClass("cas");
			$("#tab-body-content>div").hide();
			$("#tab-body-content ." + newTab).show();
			vm.getFields(newTab);
		}
	},
	//基础数据
	getBaseFields: function  (){
	return [
		{
			name: '产品',
			isOpen: true,
			data: [
				{name: '产品名称',codeText: '{{sample.sampname}}',belong: 'report,record'},
				{name: '型号规格',codeText: '{{sample.carmodel}}',belong: 'report,record'},
				{name: '商标',codeText: '{{sample.sampversion}}',belong: 'report,record'},
				{name: '受检单位',codeText: '{{sample.customername}}',belong: 'report,record'},
				{name: '生产单位',codeText: '{{sample.sampproducer}}',belong: 'report,record'},
				{name: '送样者',codeText: '{{sample.sender}}',belong: 'report,record'},
				{name: '送样日期',codeText: '{{-sample.senddate}}',belong: 'report,record'},
				{name: '样品数量',codeText: '{{sample.sampnumber}}',belong: 'report,record'},
				{name: '样品编号',codeText: '{{sample.sampcode}}',belong: 'report,record'},
				{name: '出厂编号',codeText: '{{sample.sampdeliverycode}}',belong: 'report,record'},
				{name: '样品状况',codeText: '{{sample.sampcondition}}',belong: 'report,record'},
				{name: '生产日期',codeText: '{{-sample.producedate}}',belong: 'report,record'}
			]
		},
		{
			name: '客户',
			isOpen: true,
			data: [
				{name: '客户名称',codeText: '{{custOrg.name}}',belong: 'report,record'},
				{name: '客户地址',codeText: '{{custOrg.address}}',belong: 'report,record'},
				{name: '电话',codeText: '{{custOrg.phone}}',belong: 'report,record'},
				{name: '邮政编码',codeText: '{{custOrg.postcode}}',belong: 'report,record'},
				{name: '传真',codeText: '{{custOrg.fax}}',belong: 'report,record'}
			]
		},
		{
			name: '报告',
			isOpen: true,
			data: [
				{name: '编号',codeText: '{{reportNo}}',belong: 'report'},
				{name: '编号',codeText: '{{report.reportNo}}',belong: 'record'}
			]
		},
		{
			name: '任务',
			isOpen: true,
			data: [
				{name: '承接部门',codeText: '{{dutyOrgName}}',belong: 'report,record'}
			]
		},
		{
			name: '公共',
			isOpen: true,
			data: [
				{name: '检测地点',codeText: '{{testAddress}}',belong: 'report,record'},
				{name: '设备使用记录',codeText: '{{assetsUse}}',belong: 'report,record'},
				{name: '实验人员',codeText: '{{tester}}',belong: 'report,record'},
				{name: '检测开始时间',codeText: '{{-testStartTime}}',belong: 'report,record'}
			]
		},
		{
			name: '签名',
			isOpen: true,
			data: [
				{name: '主检',codeText: '{{majorPosition}}',belong: 'report,record'},
				{name: '审核人',codeText: '{{approverPosition}}',belong: 'report,record'},
				{name: '批准人',codeText: '{{authorizerPosition}}',belong: 'report,record'},
				{name: '签发日期',codeText: '{{-authorizeTime}}',belong: 'report,record'}
			]
		},
		{
			name: '公章',
			isOpen: true,
			data: [
				{name: '首页公章',codeText: '{{1041}}',belong: '#'},
				{name: '签发日期公章',codeText: '{{1042}}',belong: '#'}
			]
		}
	];
},
};
